<template>
	<view>
		<view class="qiehuan-view" v-if="roleName === 'parent'">
			<label style="color: black;  margin-right: 30rpx;">{{currentStudent.studentName}}</label>
			<view v-if="stuList.length > 1" style="display: flex;" @click="openPopView">
				<label style="color: #03A9F4;">切换学生</label>
				<image class="qiehuan-image-view" src="../../static/icon/qiehuan.png"></image>
			</view>
		</view>

		<deptManagerPanel v-if="roleName === '部门经理'" topTitle="昨日统计" type="yesterday"></deptManagerPanel>

		<deptManagerPanel v-if="roleName === '部门经理'" topTitle="今日统计" type="today"></deptManagerPanel>

		<uni-section :title="item.text" type="line" v-for="(item, index) in menuList" :index="index" :key="index" style="margin-top: 20rpx;">
			<uni-grid :column="4" :showBorder="false"  :square="false">
				<uni-grid-item v-for="(item1, index1) in item.item" :index="index1" :key="index1">
					<view class="grid-item-box" @click="handleClick(item1)">
						<image :src="item1.img" style="width: 70rpx;height: 70rpx;"></image>
						<text class="text">{{item1.text}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</uni-section>

		<uni-popup ref="popup">
			<uni-list>
				<uni-list :border="true">
					<uni-list-item style="width: 500rpx; "  :title='item.studentName' :right-text='item.phone' clickable="true"  @click="onPopup(item)"  v-for="item in stuList" class="title">
					</uni-list-item>
				</uni-list>
			</uni-list>
		</uni-popup>
	</view>
</template>

<script>
	import {parentMenu, studentMenu, teacherMenu, dormitoryMenu, deptManagerMenu, securityStaffMenu, instructorMenu, maintenance, clerkMenu} from '../../js/menu';
	import deptManagerPanel from '../component/deptManagerPanel.vue';
	import login from "../../api/modules/index.js"
	import index from "@/api/modules/index.js"
	import { setLoginUserStorage } from "../../js/storage.js";

	export default {
		data() {
			return {
				menuList : [],
				roleName : '',
				currentStudent: {},
				stuList: []
			}
		},
		components : {
			deptManagerPanel
		},
		onShow() {
			let roleName = uni.getStorageSync("roleName");
			if(roleName === 'student') {
				this.initStudentInfo()
				console.log('on show')
			}
		},
		onLoad() {
			console.log("on load")
			
			let loginId = uni.getStorageSync("loginId");

			this.roleName = uni.getStorageSync("roleName");
			if(this.roleName === 'parent') {
				const studentList = uni.getStorageSync("identity")
				var students = JSON.parse(studentList);
				console.log("student", students)
				this.stuList = students
				this.stuList.forEach((item, index) => {
					if(item.id === loginId) {
						this.currentStudent = item
					}
				})
			}
			if(loginId === null || loginId === '') {
				this.initUser();
			}else{
				this.getMenu();
			}
		},
		methods: {
			onPopup(item) {
				this.$refs.popup.close()
				this.currentStudent = item
				uni.setStorageSync("loginId", item.id)
				this.$forceUpdate()
			},
			openPopView() {
				this.$refs.popup.open('center')
			},
			handleClick(item) {
				console.log('click Item')
				if(item.text === '退出') {
					// 清楚缓存
					uni.showModal({
						content: '确认退出?',
						title: '提示',
						success: (res) => {
							if (res.confirm) {
								uni.clearStorageSync()
								uni.reLaunch({
									url: '/pages/index/login'
								})
								return;
							}
						}
					})
				} else {
					uni.navigateTo({
						url: item.url
					})
				}
			},
			initUser() {
				var self =this;
				uni.login({
					provider: 'weixin',
					success: function (loginRes) {
						if (loginRes.errMsg !== 'login:ok') {
							return;
						}

						login.loginByOpenId(loginRes.code).then((res) => {
							if(res.data) {
								setLoginUserStorage(res);
								self.getMenu();
							}else {
								uni.reLaunch({
									url: "./login"
								})
							}
						}).catch(error => {
							console.error('登录失败！:', error)
							uni.showToast({
							  title: '登录异常',
							  icon: "error"
							})
						})
					}
				})
			},
			initStudentInfo() {
				let loginId = uni.getStorageSync("loginId");
				let token =  uni.getStorageSync("token");
				login.getUserInfoById(loginId).then((res) => {
					if(res.data) {
						res.data.token = token
						setLoginUserStorage(res);
						self.getMenu();
					}
				}).catch(error => {
			
				})
			},
			getMenu() {
				let roleName = uni.getStorageSync("roleName");
				console.log("用户角色：",roleName);
				this.roleName = roleName;

				if(roleName === 'student') {
					this.menuList = studentMenu;
				} else if(roleName === 'parent') {
					this.menuList = parentMenu;
				} else if(roleName === '班主任') {
					this.menuList = teacherMenu;
				} else if(roleName === '部门经理') {
					console.log("dept", deptManagerMenu)
					this.menuList = deptManagerMenu;
				} else if(roleName === '维修管理员') {
					this.menuList = maintenance;
				} else if(roleName === '宿管老师') {
					this.menuList = dormitoryMenu;
				} else if(roleName === '教官') {

				} else if(roleName === '保安') {
					this.menuList = securityStaffMenu;
				} else if (roleName === '教员') {
					this.menuList = instructorMenu
				}else if (roleName === '管理员') {
					this.menuList = clerkMenu
				}
				const tuichu = {
					text: '其他',
					item: [{
						img: '../../static/icon/tuichu.png',
						text : '退出',
						url : '/pages_duty/parent/dorAttendance',
						},
					]
				}

				let shallowCopy = Array.from(this.menuList)
				shallowCopy.push(tuichu)
				this.menuList = shallowCopy
				console.log('menu',this.menuList);
			}
		}
	}

</script>

<style>
	.grid-item-box {
		flex: 1;
		// position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}
	.text {
		font-size: 14px;
		margin-top: 5px;
	}
	.qiehuan-view {
		margin-bottom: 10rpx;
		margin-top: 30rpx;
		justify-content: flex-end;
		display: flex;
		margin-right: 30rpx;
	}

	.qiehuan-image-view {
		margin-left: 10rpx;
		width: 40rpx;
		height: 40rpx;
	}

</style>
